#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_pearsonhash.h"

#include "pearsonhash.c"

#ifdef COMPILE_DL_PEARSONHASH
ZEND_GET_MODULE(pearsonhash)
#endif

PHP_MINIT_FUNCTION(pearsonhash)
{
    return SUCCESS;
}

PHP_MSHUTDOWN_FUNCTION(pearsonhash)
{
    return SUCCESS;
}

PHP_RINIT_FUNCTION(pearsonhash)
{
    return SUCCESS;
}

PHP_RSHUTDOWN_FUNCTION(pearsonhash)
{
    return SUCCESS;
}

PHP_MINFO_FUNCTION(pearsonhash)
{
    php_info_print_table_start();
    php_info_print_table_header(2, "pearsonhash support", "enabled");
    php_info_print_table_row(2, "extension version", PHP_PEARSONHASH_VERSION);
    php_info_print_table_row(2, "pearsonhash release", "http://code.google.com/p/php-pearsonhash/");
    php_info_print_table_end();
}

PHP_FUNCTION(pearsonhash8)
{
    char *arg1 = NULL;
    char *ret1 = NULL;
    int arg1_len;
    unsigned char sum;

    /* parse the parameters */
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg1, &arg1_len) == FAILURE || arg1_len < 1)
    {
        RETURN_NULL();
    }

    /* compute the checksum */
        sum = phash_rfc3074(arg1, arg1_len);

    /* return the checksum */
    RETURN_LONG((long)sum);
}

PHP_FUNCTION(pearsonhash16)
{
    char *arg1 = NULL;
    char *ret1 = NULL;
    int arg1_len;
    unsigned short sum;

    /* parse the parameters */
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg1, &arg1_len) == FAILURE || arg1_len < 1)
    {
        RETURN_NULL();
    }

    /* compute the checksum */
    sum = phash16(arg1, arg1_len);

    /* return the checksum */
    RETURN_LONG((long)sum);
}

zend_function_entry pearsonhash_functions[] = {
    PHP_FE(pearsonhash8, NULL)
        PHP_FE(pearsonhash16, NULL)
    {NULL, NULL, NULL}
};

zend_module_entry pearsonhash_module_entry = {
    STANDARD_MODULE_HEADER,
    "pearsonhash",
    pearsonhash_functions,
    PHP_MINIT(pearsonhash),
    PHP_MSHUTDOWN(pearsonhash),
    NULL,
    NULL,
    PHP_MINFO(pearsonhash),
    PHP_PEARSONHASH_VERSION,
    STANDARD_MODULE_PROPERTIES
};

